package cn.mugutu.finance.controller;

import cn.mugutu.finance.domain.BaiJiaContent;
import cn.mugutu.finance.dto.BaseResult;
import cn.mugutu.finance.query.BaiJiaQuery;
import cn.mugutu.finance.query.DoubleBallQuery;
import cn.mugutu.finance.service.BaiJiaContentService;
import cn.mugutu.finance.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自媒体-百家号
 * Created by gan on 2019/2/13 22:36.
 */
@Controller
@RequestMapping("/baijia")
public class BaiJiaController extends BaseController {

    @Autowired
    HttpServletRequest request;

    @Autowired
    BaiJiaContentService baiJiaContentService;

    @RequestMapping("/listUI")
    public String listUI() {
        return "baijia/list";
    }


    @RequestMapping("/list")
    @ResponseBody
    public BaseResult.EasyUIResult<BaiJiaContent> list(BaiJiaQuery query, int page, int rows) {
        setEasyUIParamsInQuery(query, page, rows);
        Pager<BaiJiaContent> pager = baiJiaContentService.findByPage(query);
        return new BaseResult.EasyUIResult(pager.getTotalCount(), pager.getRows());
    }

    /**
     * 手动发布本地
     */
    @RequestMapping("/publishLocalUI")
    public String publishLocalUI() {
        return "baijia/publishLocal";
    }

    /**
     * 网络热文抓取
     */
    @RequestMapping("/networkFetchUI")
    public String networkFetchUI() {
        Map<String, String> map = null;
        try {
            map = baiJiaContentService.fetchList();
        } catch (Exception e) {
            System.out.println("================抓取出错啦,一下是报错信息====>");
            e.printStackTrace();
        }

        if (map == null) {
            map = new HashMap<>();
            map.put("无数据", "无数据");
        }
        request.setAttribute("map", map);
        return "baijia/networkFetch";
    }
}
